package fei.yuzhi.com.loveflowers.fragment.mefragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fei.yuzhi.com.loveflowers.R;
import fei.yuzhi.com.loveflowers.base.BaseFragment;
import fei.yuzhi.com.loveflowers.fragment.mefragment.login.LoginActivity;
import fei.yuzhi.com.loveflowers.fragment.mefragment.meSet.SetActivity;
import fei.yuzhi.com.loveflowers.fragment.mefragment.problem.ProblemActivity;
import fei.yuzhi.com.loveflowers.utils.ActivityStackManagerUtils;


/**
 * Created by Administrator on 2016/8/9.
 */
public class MeFragment extends BaseFragment implements MeContract.FragmentMe {

    @BindView(R.id.iv_me_login)
    ImageView ivMeLogin;
    @BindView(R.id.tv_me_login)
    TextView tvMeLogin;
    @BindView(R.id.linl_me_head_icon)
    LinearLayout linlMeHeadIcon;
    @BindView(R.id.iv_me_set)
    ImageView ivMeSet;
    @BindView(R.id.iv_me_order_icon)
    ImageView ivMeOrderIcon;
    @BindView(R.id.linl_me_order)
    RelativeLayout linlMeOrder;
    @BindView(R.id.iv_me_redpacket_icon)
    ImageView ivMeRedpacketIcon;
    @BindView(R.id.tv_me_redpacket_text)
    TextView tvMeRedpacketText;
    @BindView(R.id.linl_me_redpacket)
    RelativeLayout linlMeRedpacket;
    @BindView(R.id.iv_me_coupon_icon)
    ImageView ivMeCouponIcon;
    @BindView(R.id.tv_me_coupon_text)
    TextView tvMeCouponText;
    @BindView(R.id.linl_me_coupon)
    RelativeLayout linlMeCoupon;
    @BindView(R.id.iv_me_adress_icon)
    ImageView ivMeAdressIcon;
    @BindView(R.id.tv_me_adress_text)
    TextView tvMeAdressText;
    @BindView(R.id.linl_me_adress)
    RelativeLayout linlMeAdress;
    @BindView(R.id.iv_me_message_icon)
    ImageView ivMeMessageIcon;
    @BindView(R.id.tv_me_message_text)
    TextView tvMeMessageText;
    @BindView(R.id.linl_me_message)
    RelativeLayout linlMeMessage;
    @BindView(R.id.iv_me_collect_icon)
    ImageView ivMeCollectIcon;
    @BindView(R.id.tv_me_collect_text)
    TextView tvMeCollectText;
    @BindView(R.id.linl_me_collect)
    RelativeLayout linlMeCollect;
    @BindView(R.id.iv_me_inform_icon)
    ImageView ivMeInformIcon;
    @BindView(R.id.tv_me_inform_text)
    TextView tvMeInformText;
    @BindView(R.id.linl_me_inform)
    RelativeLayout linlMeInform;
    @BindView(R.id.iv_me_problem_icon)
    ImageView ivMeProblemIcon;
    @BindView(R.id.tv_me_problem_text)
    TextView tvMeProblemText;
    @BindView(R.id.linl_me_problem)
    RelativeLayout linlMeProblem;
    @BindView(R.id.iv_me_tel_icon)
    ImageView ivMeTelIcon;
    @BindView(R.id.tv_me_tel_text)
    TextView tvMeTelText;
    @BindView(R.id.linl_me_tel)
    RelativeLayout linlMeTel;
    @BindView(R.id.tv_me_daifukuan)
    TextView tvMeDaifukuan;
    @BindView(R.id.tv_me_daifahuo)
    TextView tvMeDaifahuo;
    @BindView(R.id.tv_me_daishouhuo)
    TextView tvMeDaishouhuo;
    @BindView(R.id.tv_me_daipingjia)
    TextView tvMeDaipingjia;
    @BindView(R.id.tv_me_tel_right)
    ImageView tvMeTelRight;
    private static boolean loginStatic = false;

    public static boolean isLoginStatic() {
        return loginStatic;
    }

    public static void setLoginStatic(boolean loginStatic) {
        MeFragment.loginStatic = loginStatic;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);

        ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);

        return view;
    }

    @Override
    public void onVerticalSucess() {

    }

    @Override
    public void onVerticalFail(String msg) {

    }

    @OnClick({R.id.iv_me_login, R.id.tv_me_login, R.id.iv_me_set, R.id.linl_me_order,
            R.id.linl_me_redpacket, R.id.linl_me_coupon, R.id.linl_me_adress,
            R.id.linl_me_message, R.id.linl_me_collect, R.id.linl_me_inform,
            R.id.linl_me_problem, R.id.linl_me_tel,R.id.tv_me_daifukuan,
            R.id.tv_me_daifahuo, R.id.tv_me_daishouhuo, R.id.tv_me_daipingjia})
    public void onClick(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.iv_me_login:
            case R.id.tv_me_login:
                if (loginStatic){

                }else {
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }

                break;
            case R.id.iv_me_set:
                 intent = new Intent(getContext(), SetActivity.class);
                startActivity(intent);

                break;
            case R.id.linl_me_order:
                if (loginStatic) {

                } else {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                     intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.linl_me_redpacket:
                if (loginStatic) {

                } else {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.linl_me_coupon:
                if (loginStatic) {

                } else {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.linl_me_adress:
                if (loginStatic) {

                } else {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.linl_me_message:
                if (loginStatic) {

                } else {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.linl_me_collect:
                if (loginStatic) {

                } else {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.linl_me_inform:
                if (loginStatic) {

                } else {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.linl_me_problem:
                 intent = new Intent(getContext(), ProblemActivity.class);
                startActivity(intent);
                break;
            case R.id.linl_me_tel:
                Uri uri = Uri.parse("tel:4008655222");
                 intent = new Intent(Intent.ACTION_DIAL, uri);
                startActivity(intent);
                break;
            case R.id.tv_me_daifukuan:
                if (loginStatic) {

                } else {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.tv_me_daifahuo:
                if (loginStatic) {

                } else {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.tv_me_daishouhuo:
                if (loginStatic) {

                } else {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.tv_me_daipingjia:
                if (loginStatic) {

                } else {
                    Toast.makeText(getContext(), "请先登录", Toast.LENGTH_SHORT).show();
                    intent = new Intent(getContext(), LoginActivity.class);
                    startActivity(intent);
                }
                break;

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getUserName(String name) {
        tvMeLogin.setText(name);
        ActivityStackManagerUtils.getInstance().finishActivity(LoginActivity.class);
        Toast.makeText(getContext(), "登陆成功", Toast.LENGTH_SHORT).show();
        loginStatic = true;
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getLoginStatic(Boolean b) {
        tvMeLogin.setText(R.string.me_login);
        Toast.makeText(getContext(), "已退出登陆", Toast.LENGTH_SHORT).show();
        loginStatic = b;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


}
